package com.tc.kqdemo.quartz;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tc.kqdemo.pojo.ConfigData;
import com.tc.kqdemo.pojo.Data;
import com.tc.kqdemo.service.DataService;
import com.tc.kqdemo.service.DingDing;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Configuration
public class TestQuartz {

    @Autowired
    private DataService dataService;
    @Autowired
    private ConfigData t;

    //内网服务器向外网服务器发送数据
    public void sendData(){
        System.out.println("内网>>>>>>>>>外网推送数据开始!!!!!!!"+new Date());
         //查询需要推送的数据
        SimpleDateFormat d2 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String date_ = d2.format(date);
        //查询需要当天写进去的数据
        List<Data> msgList = dataService.checkSendingData(date_, "false");
        System.out.println("需要传输的数据:"+JSON.toJSONString(msgList)+"<============");
        if(msgList.size()>0){
            for (Data d:msgList) {
                d.setAddress_("国华太仓发电有限公司");
            }
            String sendUrl = t.getSendUrl();
            System.out.println("传输的路径========>:"+sendUrl);
            //String url="http://180.103.210.20:8081/pro3/pro/receive";
            HttpPost httpPost = new HttpPost(sendUrl);
            RequestConfig config = RequestConfig.custom().setSocketTimeout(100000).setConnectTimeout(100000).build();
            CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
            JSONObject re = new JSONObject();
            re.put("data",msgList);
            re.put("code",0);
            httpPost.setConfig(config);
            StringEntity en = new StringEntity(re.toString(), "utf-8");
            en.setContentType("application/json");
            httpPost.setEntity(en);
            CloseableHttpResponse response=null;
            JSONObject result=null;
            try {
                response = httpClient.execute(httpPost);
                int code = response.getStatusLine().getStatusCode();
                if (code==HttpStatus.SC_OK){
                    System.out.println("外网服务器正常响应中!!!!!!!!!!!");
                    result = JSON.parseObject(EntityUtils.toString(response.getEntity()));
                }
            } catch (IOException e) {
                System.out.println(e.getMessage()+"<<<<<<传输错误的原因");
                e.printStackTrace();
            }finally {
                System.out.println(JSON.toJSONString(result)+"<---------------返回的结果,"+new Date());
                if(result!=null&&result.getInteger("code")==0){
                    System.out.println("传输数据成功!!!!");
                    for (Data d:msgList) {
                        int num2 = dataService.up_isSend(d.getId());
                        if (num2>0){
                            System.out.println("更新isSend状态成功!!!!!!"+d.getId());
                        }else{
                            System.out.println("更新isSend状态失败!!!!!!"+d.getId());
                        }
                    }
                }
                try {
                    if (response!=null){
                        response.close();
                    }
                    if (httpClient!=null){
                        httpClient.close();
                    }
                    System.out.println("HTTP-POST-请求发送考勤数据结束!!!!!!!!!!"+new Date());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            en=null;
            re=null;
            date=null;
            d2=null;
        }else{
            System.out.println("没有需要传输的数据!!!!!!!");
        }
        System.out.println("内网>>>>>>>>>外网推送数据开结束!!!!!!"+new Date());
    }




    //再次写入考勤失败的数据
    public void dd_1(){
        System.out.println("再次写入考勤数据开始!!!!"+new Date());
//        SimpleDateFormat d1 = new SimpleDateFormat("yyyy-MM-dd");
//        String date = d1.format(new Date());
//        List<Data> list = dataService.checkDataByDate(date);
//        System.out.println(list.size()+"<----------未写入考勤的数据长度");
//        if (list.size()>0){
//            String access_token = dd.getAccess_token();
//            for (int i = 0; i <list.size(); i++) {
//                Data d = list.get(i);
//                boolean b = dd.readIntoData(d.getUserid(), d.getAddress_(), d.getEquipid(), d.getPic_address(), d.getTime2().getTime(), access_token);
//                if (b){
//                    System.out.println("写入考勤打卡成功!!!!");
//                    int num_2 = dataService.up_isSuccess(d.getId());
//                    if (num_2>0){
//                        System.out.println("更新=个人==考勤打卡结果成功!!!!!!");
//                    }else{
//                        System.out.println("更新=个人==考勤打卡结果失败!!!!!!");
//                    }
//
//                }else{
//                    System.out.println("写入考勤打卡失败!!!!!======"+d.getUserid());
//                }
//            }
//        }
//        d1=null;
        System.out.println("再次写入考勤数据结束!!!!"+new Date());
    }



}
